//
//  MXGFirstPageCell.m
//  weibo
//
//  Created by DP on 16/7/5.
//  Copyright © 2016年 DP. All rights reserved.
//

#import "XMGFirstPageCell.h"
#import "XMGFirstPageItem.h"
#import "NSDate+DP.h"
#import "NSString+DP.h"
#import <UIImageView+WebCache.h>
#import "UIImage+DP.h"

@interface XMGFirstPageCell ()
/**头像*/
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
/**文章来源*/
@property (weak, nonatomic) IBOutlet UILabel *originlabel;
/**Vip*/
@property (weak, nonatomic) IBOutlet UIImageView *vipImageView;
/**时间*/
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
/**微博发布者*/
@property (weak, nonatomic) IBOutlet UILabel *nameLbel;
/**文字内容*/
@property (weak, nonatomic) IBOutlet UILabel *contextLabel;
/**配图*/
@property (weak, nonatomic) IBOutlet UIImageView *pictureImageView;
/**位置*/
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
/**配图据底部距离*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottomConstraint;
/**文字内容据底部距离*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentLabelBottemConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *originLabelToIconImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *timeToIconImage;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *locationBottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *locatiinHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureHeight;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *midBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

@end





@implementation XMGFirstPageCell

- (void)setItem:(XMGFirstPageItem *)item {
    _item = item;
    //名称
    self.nameLbel.text = item.name;
//    [self.nameLbel sizeToFit];
    //内容
    self.contextLabel.text = item.text;
//    [self.contextLabel sizeToFit];
    //来源
    self.originlabel.text = item.source;
//    [self.originlabel sizeToFit];
    //时间
    if (item.creat_at) {
        NSDate *now = [NSDate new];
        NSDate *publishTime = [NSString SrtingToDate:item.creat_at];
        NSTimeInterval timeInterval = [now timeIntervalSinceDate:publishTime]/3600;
        NSString *timeStr = [NSString stringWithFormat:@"%f小时前",timeInterval];
        self.timeLabel.text = timeStr;
    }else {
        self.originLabelToIconImageView.constant = 10;
        self.timeToIconImage.constant = 0;
    }
    //位置
    if (item.location) {
        self.locationLabel.text = item.location;
    }else {
        self.locatiinHeight.constant = 0;
        self.locationBottom.constant = 0;
        self.pictureBottomConstraint.constant = 0;
    }
    
    //头像
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:item.profile_image_url]];
//  self.iconImageView.image = [UIImage clipImageToCircl:self.iconImageView.image];
    //配图
    if (item.bmiddle_pic) {
       self.pictureImageView.hidden = NO;
        [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:item.bmiddle_pic]];
    }else {
        self.pictureHeight.constant = 0;
        self.pictureImageView.hidden = YES;
        self.pictureBottomConstraint = 0;
        self.contentLabelBottemConstraint.constant = 0;
    }
    
  
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //裁剪头像为圆形
     self.iconImageView.image = [UIImage clipImageToCircl:self.iconImageView.image];
    //设置图片填充模式
    self.pictureImageView.contentMode = UIViewContentModeScaleAspectFit;
    //设置按钮边框
    self.leftBtn.layer.borderWidth = 1;
    self.leftBtn.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    self.rightBtn.layer.borderWidth = 1;
    self.rightBtn.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    self.midBtn.layer.borderWidth = 1;
    self.midBtn.layer.borderColor = [[UIColor lightGrayColor]CGColor];


    
}

/**xib 和 storyboard设置好约束，不需要计算高度，纯代码才需要*/
/*
- (CGFloat)updateHeight
{
    [self layoutIfNeeded];
    CGFloat height;
    if (self.pictureImageView == nil)
    {
        if (self.locationLabel == nil)
        {
            
            height = 50 + self.textLabel.frame.size.height +30;
        }else {
            height = 50 +self.textLabel.frame.size.height + 8 +self.locationLabel.frame.size.height + 40;
        }
    }else
    {
        
        if (self.locationLabel == nil)
        {
            height = 50 +self.textLabel.frame.size.height + 10 +self.pictureImageView.frame.size.height + 30;
            
        }else
        {
             height = 50 +self.textLabel.frame.size.height + 10 +self.pictureImageView.frame.size.height + 8+ self.locationLabel.frame.size.height+ 40;
        }

    }
    return height;
}

*/

- (void)awakeFromNib {
    [super awakeFromNib];

    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
    
}

@end
